/**
 * @file StreamReader.h
 *
 * This file defines the StreamReader interface.
 *
 * @author Luc Fourestier
 * @date 2009
 */

#ifndef STREAMREADER_H_
#define STREAMREADER_H_

#include "Global.h"
#include "CircBuff.h"
#include "StreamAnalyser.h"

#include <netinet/in.h>
#include <pthread.h>

#define SR_MAX_NAME_LENGTH MAX_NAME_LENGTH
#define SR_MAX_FILE 50

// Errors
#define SR_ERROR_BAD_PARAM       1 //!< Incorrect parameters
#define SR_ERROR_CANNOT_ALLOCATE 2 //!< Cannot create/allocate object required by the StreamReader
#define SR_ERROR_NOT_SUPPORTED   3 //!< Input type is not supported yet
#define SR_ERROR_FILE_PB		 4 //!< Error while opening file



/**
 * Specify the option to be used.
 *
 */
typedef struct SR_Option_s
{
	InOutType_t InputType;						//!< The input type
	char FileName[SR_MAX_FILE][SR_MAX_NAME_LENGTH];  		//!< The file(s) from where to read (File InputType only)
	char RcvAddress[IP_ADDR_LENGTH];  			//!< The reception address (multicast address to listen)
	unsigned int RcvPort;  						//!< The reception port to listen
	char BindAddress[IP_ADDR_LENGTH];  			//!< The bind address, the interface where to listen
	CircBuff_t * CB;							//!< The circular buffer where to push the stream chunks
	struct timeval ReferenceTime; 				//!< The reference time for timestamps
} SR_Option_t;

/**
 * The StreamReader info structure
 */
typedef struct SR_StreamReader_s
{
	SR_Option_t Options;						//!< The option for this StreamReader
	struct sockaddr_in LocalAddr;				//!< The local interface
	struct sockaddr_in ClientAddr;				//!< The remote address
	int Socket;									//!< The reception socket
	unsigned int RcvBuffSize;					//!< The reception socket buffer size
	unsigned int TotalByteRead;					//!< The total amount of bytes received
	int ListenSocket;							//!< The TCP listen socket
	FILE * InputFile[SR_MAX_FILE];				//!< The file from where to read
	unsigned int CurrentFile;					//!< The number of the currently read file in the playlist
	unsigned int FileNumber;					//!< The total number of file in the playlist (FileName array)
	pthread_t ThreadID; 						//!< The ID of the StreamReader thread
	unsigned char ThreadLoop;					//!< The infinite loop of the thread

#ifdef DEBUG_SR_PROFILING
	struct timeval TotalPassDuration;			//!< The sum of all pass duration (a pass is one thread round)
	unsigned long long passCount;				//!< The number of pass
	unsigned long long passError;				//!< The number of pass error
#endif
} SR_StreamReader_t;

/**
 * @brief Create the StreamReader
 */
int SR_Create(SR_StreamReader_t ** SR_p, SR_Option_t Options);

/**
 * @brief Delete the StreamReader
 */
int SR_Delete(SR_StreamReader_t * SR);

/**
 * @brief Print the StreamReader
 */
int SR_Print(SR_StreamReader_t * SR, char * Name, int Level, FILE * Output);

#endif /* STREAMINPUT_H_ */
